<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        mode="search"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          icon="search"
          @click="search()"
        >搜索</a-button>
        <a-button
          v-if="!isReadOnly"
          class="action-button"
          type="primary"
          icon="plus"
          auth="member-grain.add-field"
          @click="handlerTableAdd()"
        >新增耕地信息</a-button>
        <!-- <a-button
          v-if="!isReadOnly"
          class="action-button"
          type="primary"
          auth="member-grain.field-into"
          @click="handlerInto()"
        >转入耕地</a-button> -->
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
      
    </section>
    <a-alert :message="`上报合计${currentField.field}亩，下列累计${fieldTotal.field}亩，承包地：${fieldTotal.field1}，增加地：${fieldTotal.field2}，减少地：${fieldTotal.field3}`" type="info"></a-alert>
    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="record => record.usr_id"
      :loading="tableLoading"
      :pagination="false"
      :customRow="customTableRow"
      :scroll="{
        x: '100%',
        y: 500
      }"
      bordered
    >
      <template
        slot="field_type"
        slot-scope="row"
      >
        {{getLabel(row.field_type, [], 'field_type')}}
      </template>
      <template
        slot="field_from"
        slot-scope="row"
      >
        {{getLabel(row.field_from, [], 'field_from')}}
      </template>

      <template
        slot="field_size"
        slot-scope="row"
      >
        {{(+row.field_from === 3 ? '-' : '') + row.field_size}}
      </template>
      
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="link"
          v-if="!isReadOnly"
          size="small"
          auth="member-grain.edit-field"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            type="link"
            size="small"
            auth="member-grain.field-del"
          >删除</a-button>
        </a-popconfirm>
        <!-- <a-button
          class="action-button"
          type="link"
          v-if="!isReadOnly && +row.field_from !== 3"
          size="small"
          @click="handlerReduce(row)"
        >减少耕地</a-button> -->
        <!-- <a-button
          class="action-button"
          type="link"
          v-if="!isReadOnly"
          size="small"
          @click="handlerReduce(row)"
        >分割或转出</a-button> -->
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      centered
      @cancel="visible = false"
      @ok="handleSubmit()"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="customMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
    </a-modal>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { userServices, IAdmin } from '@/common/services'
import { famerServices, IMember, IGrain, IField } from '@/common/services/famer'
import { commonServices } from '@/common/services/common'
import { memberRole } from '@/common/enum'
import { publishServices } from '@/common/services/publish'
import Into from './Into.vue'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
    Into
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: Object
  })
  public currentField!: IGrain
  @Prop({
    type: [Number, String]
  })
  public currentFieldType!: number | string

  @Prop({
    type: Boolean
  })
  public isReadOnly!: boolean

  @Prop({
    type: String
  })
  public currentPublishId!: string

  public commonParams = {
    s_a_id: '',
    family_id: '',
    pub_id: this.currentPublishId
  }

  // 产品列表搜索条件
  public tableSearch = {
    field_from: '',
    field_type: ''
  }

  public customMode = ''

  public formItemLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 17 }
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '记录号',
      key: 'field_id',
      dataIndex: 'field_id',
      width: 160,
    },
    {
      title: '耕地编号',
      key: 'field_id2',
      dataIndex: 'field_id2',
      width: 160,
    },
    {
      title: '耕地类型',
      scopedSlots: { customRender: 'field_type' },
      key: 'field_type',
      width: 80,
      form: {
        type: 'a-radio-group',
        validate: '*',
        isShowQuery: true,
        renderMode: ['add', 'edit'],
        // disabled: ['edit'],
        placeholder: '请选择耕地类型',
        dict: [
          {
            label: '田',
            value: '1'
          },
          {
            label: '土',
            value: '2'
          }
        ]
      }
    },
    // 列表专用
    {
      title: '耕地性质',
      scopedSlots: { customRender: 'field_from' },
      key: 'field_from',
      width: 80,
      form: {
        type: 'a-radio-group',
        isShowQuery: true,
        isShowFrom: false,
        dict: [
          {
            label: '承包地',
            value: '1'
          },
          {
            label: '增加面积',
            value: '2'
          },
          {
            label: '减少面积',
            value: '3'
          }
        ]
      }
    },


    {
      title: '耕地性质',
      scopedSlots: { customRender: 'field_from' },
      key: 'field_from',
      width: 80,
      form: {
        type: 'a-radio-group',
        isShowList: false,
        // disabled: ['edit'],
        renderMode: ['add', 'edit'],
        dict: [
          {
            label: '承包地',
            value: '1'
          },
          {
            label: '增加面积',
            value: '2'
          },
          {
            label: '减少面积',
            value: '3'
          }
        ]
      }
    },
    {
      title: '确权面积（亩）',
      // dataIndex: 'field_size',
      scopedSlots: { customRender: 'field_size' },
      key: 'field_size',
      width: 120,
      align: 'right',
      form: {
        type: 'a-input',
        // disabled: ['edit'],
        placeholder: '请输入确权面积',
        renderMode: ['add', 'edit'],
        validate: '*',
      }
    },
    {
      title: '地理位置（经度）',
      dataIndex: 'field_long',
      key: 'field_long',
       width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入经度',
        renderMode: ['add', 'edit']
      }
    },
    {
      title: '地理位置（纬度）',
      dataIndex: 'field_law',
      key: 'field_law',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入纬度',
        renderMode: ['add', 'edit']
      }
    },
    {
      title: '耕地来源或去向',
      dataIndex: 'readme_sys',
      key: 'readme_sys',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '',
        isShowFrom: false
      }
    },
    // {
    //   title: '接收人',
    //   dataIndex: 'readme',
    //   key: 'readme',
    //   form: {
    //     type: 'a-input',
    //     placeholder: ''
    //   }
    // },
    {
      title: '备注',
      dataIndex: 'readme',
      key: 'readme',
      ellipsis: true,
      width: 150,
      form: {
        type: 'a-textarea',
        placeholder: '请输入备注',
        renderMode: ['add', 'edit']
      }
    },

    // 减少专用
    {
      title: '确权面积（亩）',
      dataIndex: 'field_size',
      key: 'field_size',
      width: 120,
      align: 'right',
      form: {
        type: 'a-input',
        placeholder: '输入减少的确权面积',
        isShowList: false,
        renderMode: ['del']
      }
    },
    {
      title: '备注',
      dataIndex: 'readme',
      key: 'readme',
      form: {
        type: 'a-textarea',
        placeholder: '输入减少面积的原因',
        isShowList: false,
        renderMode: ['del']
      }
    },
  
    {
      title: '操作',
      key: 'action',
      width: 180,
      fixed: 'right',
      form: {
        isShowList: !this.isReadOnly,
        isShowFrom: false
      },
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: this.currentPublishId
      ? publishServices.getPublishUserFields
      : famerServices.getMemberFieldDetail,
    add: famerServices.updateMemberGrain,
    edit: famerServices.updateMemberGrain,
    del: famerServices.deleteMemberField,
    // imageUpload: 'uploadStoreImage',
    // imageRemove: 'delStoreImage'
  }

  public fieldTotal = {
    field: '',
    field1: '',
    field2: '',
    field3: '',
  }

  @Watch('currentField.family_id')
  @Watch('currentFieldType', { immediate: true })
  public handlerCurrentFieldChange() {
    this.commonParams.s_a_id = this.currentField.s_a_id
    this.commonParams.family_id = this.currentField.family_id
    this.$set(this.tableSearch, 'field_from', `${this.currentFieldType}`)
    // console.log('tableSearch', this.tableSearch.field_from)
    this.getData()
  }

  public mounted() {
  }

  public async getData() {
    const res = await this.getTableList()
    // this.tableData = res.data || []
    this.fieldTotal.field = res.data.field
    this.fieldTotal.field1 = res.data.field1
    this.fieldTotal.field2 = res.data.field2
    this.fieldTotal.field3 = res.data.field3
  }

  public handlerTableAdd() {
    this.tableAdd()
    this.customMode = 'add'
    this.visibleTitle = '添加耕地'
    this.$nextTick(() => {
      this.currentRow = Object.assign({
        field_from: `${this.currentFieldType}`
      })
    })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: IGrain) {
    this.customMode = 'edit'
    this.tableEdit(Object.assign({}, row))
    this.visibleTitle = "修改"
  }

  // 转入耕地
  public handlerInto() {
    this.$dialog(Into, {
      record: {
      },
      currentAreaId: this.currentField.s_a_id,
      currentMemberId: this.currentField.family_id,
      currentAreaName: this.currentField.s_a_nam,
      on: {
        ok () {
        },
        cancel () {
          console.log('cancel 回调')
        },
        close () {
          console.log('modal close 回调')
        }
      }
    },
    // modal props
    {
      title: '填写土地出让信息',
      width: '80%',
      centered: true,
      maskClosable: false,
      footer: null
    })
  }

  public handlerReduce(row: IField) {
    this.visible = true
    this.visibleTitle = '减少耕地（登记用于建房、修路等减少面积）'
    this.formMode = 'edit'
    this.customMode = 'del'
    this.$nextTick(() => {
      this.$refs['custom-form'].resetFields()
      this.currentRow = {
        field_type: row.field_type,
        field_from: '3',
        field_id2: row.field_id,
        field_long: row.field_long,
        field_law: row.field_law,
        readme: '',
        field_size: ''
      }
    })
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
